package com.datastructure.test.appearonceonarray;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class AppearOnceOnArray {

    public static void main(String[] args) {
        AppearOnceOnArray appearOnceOnArray = new AppearOnceOnArray();
        int[] array = {1,4,1,6};
        appearOnceOnArray.FindNumsAppearOnce(array);
    }

    public int[] FindNumsAppearOnce (int[] array) {
        // write code here
        if(array.length<=2){
            return array;
        }
        Arrays.sort(array);
        ArrayList<Integer> data = new ArrayList<>();
        for (int i = 0; i < array.length; i++) {
            if(i==0&&array[0]!=array[1]){
                data.add(array[0]);
            }
            if(i>0&&array[i]==array[i-1]){
                continue;
            }
            if(i+1<= array.length-1) {
                if (i > 0 && array[i] != array[i - 1] && array[i] != array[i + 1]) {
                    data.add(array[i]);
                }
            }
            if(i== array.length-1&&array[array.length-1]!=array[array.length-2]){
                data.add(array[array.length-1]);
            }
        }
        int [] result = new int[data.size()];
        for (int i = 0; i < result.length; i++) {
            result[i]= data.get(i);
        }
        return result;
    }

    public int singleElement (ArrayList<Integer> v) {
        // write code here
        int result = 0;
        for(Integer num:v){
            result ^= num;
        }
        return result;
    }
}
